package com.application.magazine;

import java.util.Collections;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.devspark.appmsg.AppMsg;

@SuppressLint("ValidFragment")
public class ListViewBookStand extends Fragment {
	List<Item_Magazine> arrayOfList;
	ListView listBookStand;
	Activity act;
	int typed;
	Fragment resultPage;
	public ListViewBookStand(Activity use,int type) {
		this.act = use;
		this.typed = type;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		System.gc();
		View v = null;
		v = inflater.inflate(R.layout.book_stand_list, null);
		LayoutInflater inflat = getActivity().getLayoutInflater();
		
		//listBookStand = (ListView) v.findViewById(R.id.book_stand_listview);
		//new LoadingListOfBook(act,typed).execute();
		ViewPager pager =  (ViewPager) v.findViewById(R.id.pager_book_inside);
		MagazineBookPane mAdapter = new MagazineBookPane(getFragmentManager());
		pager.setAdapter(mAdapter);
		return v;
	}

	public void setAdapterToListview() {
		NewsRowAdapter objAdapter = new NewsRowAdapter(act, R.layout.row,arrayOfList, listBookStand, getResources());
		listBookStand.setAdapter(objAdapter);
	}

	public class LoadingListOfBook extends AsyncTask<String, Void, Void> {

		ProgressDialog pDialog;
		public int sortTyp;

		public LoadingListOfBook(Activity ac, int sortTyp) {
			this.sortTyp = sortTyp;
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();

			pDialog = new ProgressDialog(act);
			pDialog.setMessage("Loading...");
			pDialog.show();

		}

		@Override
		protected Void doInBackground(String... params) {
			JSONParserForGetList json = new JSONParserForGetList();
			arrayOfList = json.getMagazineAll();
			switch (sortTyp) {
			case 1:
				try{
					Collections.sort(arrayOfList, new ComparableMagazineByTitle());
				}catch (Exception e) {
					// TODO: handle exception
				}
				break;
			case 2:

			default:
				break;
			}

			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);

			if (null != pDialog && pDialog.isShowing()) {
				pDialog.dismiss();
			}

			if (null == arrayOfList || arrayOfList.size() == 0) {
				// MagazineBookSportMain.this.finish();
				//Toast.makeText(act, "Cannot connect to server ! Please Check internet connection",Toast.LENGTH_SHORT).show();
				AppMsg appMsg = AppMsg.makeText(act, "Cannot connect to server !!", AppMsg.STYLE_ALERT);
				appMsg.setLayoutGravity(Gravity.BOTTOM);
				appMsg.show();
			} else {
				AppMsg appMsg = AppMsg.makeText(act, getResources().getString(R.string.fetching_complete), AppMsg.STYLE_INFO);
				appMsg.setLayoutGravity(Gravity.BOTTOM);
				appMsg.show();
				setAdapterToListview();
			}

		}
	}
	class MagazineBookPane extends FragmentPagerAdapter {

		private List<Item_Magazine> magList;
		

		public MagazineBookPane(FragmentManager fragmentManager) {
			super(fragmentManager);
		}

		@Override
		public Fragment getItem(int position) {			
			return BookListFragment.newInstance(getMagList(), position, getCount(), act);
		}


		@Override
		public int getCount() {
			return getMagList().size()>0 ? getMagList().size()/4 : 1;
		}

		public List<Item_Magazine> getMagList() {
			if(magList==null){
				JSONParserForGetList json = new JSONParserForGetList();
				magList = json.getMagazineAll();
			}
			
			return magList;
		}

		public void setMagList(List<Item_Magazine> magList) {
			this.magList = magList;
		}
		
	
		
		
	}
}
